package com.host.dyyl.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

/**
 * Created by Fsh on 2016/12/28.
 */

public class FileStorage {
    private Context mContext;
    //图片文件夹
    private File imageDir;
    //裁剪图片文件夹
    private File cropImageDir;
    //录音文件夹
    private File recordDir;
    //视频文件夹
    private File videoDir;

    public FileStorage(Context context) {
        this.mContext = context;
        File external;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            external = Environment.getExternalStorageDirectory();
        } else {
            external = context.getCacheDir();
        }
        String rootDir = "/" + "myimage";
        cropImageDir = new File(external, rootDir + "/crop");
        if (!cropImageDir.exists()) {
            cropImageDir.mkdirs();
        }
        imageDir = new File(external, rootDir + "/image");
        if (!imageDir.exists()) {
            imageDir.mkdirs();
        }
        recordDir = new File(external, rootDir + "/record");
        if (!recordDir.exists()) {
            recordDir.mkdirs();
        }
        videoDir = new File(external, rootDir + "/video");
        if (!videoDir.exists()) {
            videoDir.mkdirs();
        }
    }

    public File createCropFile() {
        String fileName = "";
        if (cropImageDir != null) {
            fileName = UUID.randomUUID().toString() + ".png";
        }
        return new File(cropImageDir, fileName);
    }

    public File createImageFile() {
        String fileName = "";
        if (imageDir != null) {
            fileName = UUID.randomUUID().toString() + ".jpg";
        }
        return new File(imageDir, fileName);
    }

    public String createImagePath() {
        String fileName = "";
        if (imageDir != null) {
            fileName = UUID.randomUUID().toString() + ".jpg";
        }
        return imageDir + "/" + fileName;
    }

    public String createRecordPath() {
        String fileName = "";
        if (recordDir != null) {
            fileName = UUID.randomUUID().toString() + ".amr";
        }
        return recordDir + "/" + fileName;
    }

    public String createVideoPath() {
        String fileName = "";
        if (videoDir != null) {
            fileName = UUID.randomUUID().toString() + ".mp4";
        }
        return videoDir + "/" + fileName;
    }

    public String createImagePath(String fileName) {
        return imageDir + "/" + fileName;
    }

    public String createImageName() {
        return UUID.randomUUID().toString() + ".jpg";
    }

    public File getImageDir() {
        return imageDir;
    }

    public void deleteFile(String path) {
        File file = new File(path);
        if (file.exists())
            file.delete();
    }

//    /**
//     * 获取有权限的Uri,主要用于7.0及以上系统
//     *
//     * @param context
//     * @param path
//     * @return
//     */
//    public Uri createPermissionUri(Context context, String path) {
//        File file = new File(path);
//        //android 7.0以上
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//            return FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", file);
//        } else {
//            return Uri.fromFile(file);
//        }
//    }

    public String getRealFilePath(final Uri uri) {
        if (null == uri) return null;
        final String scheme = uri.getScheme();
        String data = null;
        if (scheme == null)
            data = uri.getPath();
        else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
            data = uri.getPath();
        } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
            Cursor cursor = mContext.getContentResolver().query(uri, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null);
            if (null != cursor) {
                if (cursor.moveToFirst()) {
                    int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                    if (index > -1) {
                        data = cursor.getString(index);
                    }
                }
                cursor.close();
            }
        }
        return data;
    }

    /**
     * 复制文件
     * @param oldPath
     * @param newPath
     * @throws Exception
     */
    public void copy(String oldPath, String newPath) throws Exception {
        InputStream in = new FileInputStream(oldPath);
        OutputStream out = new FileOutputStream(newPath);
        byte[] buffer = new byte[1024];
        int len;
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }

        in.close();
        out.close();
    }
}
